﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Forms;
using AMS.DataAccess.Trademark.Interfaces;
using Spring.Context;
using Spring.Context.Support;
using AMS.ComponentModel;

namespace AMS.Trademark.Management
{
    public partial class FrmRecvFileRepository : BaseForm
    {
        public FrmRecvFileRepository()
        {
            InitializeComponent();
            cmbSource.SelectedIndex = 0;
            cmbCaseType.Items.Add("所有类型");

           ApplyDelayLoadData();
        }

        protected override void DelayLoadData()
        {
            Cursor = Cursors.WaitCursor;
            try
            {
                grid.Init("RFRepository");
                DoDataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void DoDataBind()
        {
            IApplicationContext context =  ContextRegistry.GetContext();
            IRecvFileManager rfManager = (IRecvFileManager)context.GetObject("RFManager");
            DataTable data = rfManager.GetAllCategoryRecvFiles();
            grid.DataBind(data);
            grid.RecordCount = data.Rows.Count;
        }

        private void ApplyFilter()
        {
            string filter = null;
            if (cmbSource.SelectedIndex > 0)
            {
                filter = "";
            }
            else
            {
            }

            if (cmbCaseType.SelectedIndex > 0)
            {
            }
            else
            {
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var dlg = new EditRecvFileDlg();
            if (DialogResult.OK == dlg.NewRecvFile())
            {
                DoDataBind();
            }
        }

        private void grid_SelectedRowChanged(object sender, EventArgs e)
        {
            int dataRowIndex = grid.GridView.GetSelectedRowDataIndex();
            if (dataRowIndex == -1)
            {
                return;
            }

            DataTable dt = (DataTable)grid.DataSource;
            DataRow row = dt.Rows[dataRowIndex];
            ucRecvFileDetail1.DataBind(row);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DoDataBind();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRow == null)
            {
                return;
            }

            int index = grid.GetSelectedRowDataIndex();
            DataTable dt = grid.DataSource as DataTable;
            string rfIndex = (string)dt.Rows[index]["RFIndex"];

            var dlg = new EditRecvFileDlg();
            if (DialogResult.OK == dlg.Edit(rfIndex))
            {
                DoDataBind();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(grid.SelectedRow == null)
            {
                return;
            }
            if (DialogResult.Yes != MessageBox.Show(this, "确认删除选中的收文吗？", "", MessageBoxButtons.YesNoCancel))
            {
                return;
            }

            int index = grid.GetSelectedRowDataIndex();
            DataTable dt = grid.DataSource as DataTable;
            string rfIndex = (string)dt.Rows[index]["RFIndex"];

            IRFTaskManager rftask = ObjectUtility.GetObject<IRFTaskManager>("RFTask");
            rftask.DeleteRecvFile(rfIndex);
            MessageBox.Show("删除成功");
            DoDataBind();
        }

        private void splitter_Click(object sender, EventArgs e)
        {
            ucRecvFileDetail1.Visible = !ucRecvFileDetail1.Visible;
        }

    }
}
